package org.greenrobot.eventbus.harmony;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.greenrobot.eventbus.harmony.model.IOThreadEvent;
import org.greenrobot.eventbus.harmony.model.MainThreadEvent;
import org.greenrobot.eventbus.harmony.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import org.greenrobot.eventbus.harmony.util.ToastUtil;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 主线程中回调
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThreadEvent(MainThreadEvent event) {
        ToastUtil.toast(getContext(), "Thread：" + Thread.currentThread().getName());
    }

    /**
     * IO线程中回调
     */
    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void onIOThreadEvent(IOThreadEvent event) {
        ToastUtil.toast(getContext(), "Thread：" + Thread.currentThread().getName());
    }
}
